//
//  BaseRefreshViewController.m
//  nonghubao
//
//  Created by chongqing642 on 13-3-13.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "BaseRefreshViewController.h"

@interface BaseRefreshViewController ()

@end

@implementation BaseRefreshViewController
@synthesize disableTopRefresh = _disableTopRefresh;
@synthesize disableBottomRefresh = _disableBottomRefresh;
@synthesize isFooterRefresh = _isFooterRefresh;
@synthesize delegate = _delegate;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (_disableTopRefresh == NO) {
        //创建下拉表头
        if (_refreshHeaderView && [_refreshHeaderView superview]) {
            [_refreshHeaderView removeFromSuperview];
        }
        _refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.tableView.frame.size.width, self.tableView.bounds.size.height)];
        _refreshHeaderView.delegate = self;
        
        [self.tableView addSubview:_refreshHeaderView];
        
        [_refreshHeaderView refreshLastUpdatedDate];
    }
    
    if (_disableBottomRefresh == NO) {
        //创建底部按钮
        _refreshFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
        
        //按钮
        _refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 0, 220, 44)];
        _refreshButton.titleLabel.font = _SYS_FONT_004;
        [_refreshButton setTitle:_L(@"btn_loadmore_01") forState:UIControlStateNormal];
        [_refreshButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_refreshButton addTarget:self action:@selector(refreshButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        
        
        //刷新指示
        _activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        [_activity setCenter:CGPointMake(25, 25)];//指定进度轮中心点
        [_activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        
        _reloadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(140.0f, 9.0f, 80.0f, 30.0f)];
        _reloadingLabel.text = _L(@"title_loading_01");
        _reloadingLabel.backgroundColor = [UIColor clearColor];
        _reloadingLabel.font = _SYS_FONT_004;
        
        self.tableView.tableFooterView = _refreshFooterView;
    }
}

//提供控制是否有下拉刷新功能
- (void)setDisableTopRefresh:(BOOL)disableTopRefresh
{
    
    _disableTopRefresh = disableTopRefresh;
    
    if (disableTopRefresh == NO) {
        
        _refreshHeaderView.hidden = NO;
        
    }else {
        
        _refreshHeaderView.hidden = YES;
    }
}

//提供控制是否有底部刷新功能
- (void)setDisableBottomRefresh:(BOOL)disableBottomRefresh
{
    _disableBottomRefresh = disableBottomRefresh;
    
    if (disableBottomRefresh == NO) {
        
        _refreshFooterView.hidden = NO;
   
    }else {
        
        _refreshFooterView.hidden = YES;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark Data Source Loading / Reloading Methods

- (void)reloadTableViewDataSource{
	
	//  should be calling your tableviews data source model to reload
	//  put here just for demo
    _reloading = YES;
    
    if (_delegate != nil && [_delegate respondsToSelector:@selector(reloadDataSource)]) {
        
        [_delegate reloadDataSource];
    }
    
}

- (void)doneLoadingTableViewData{
	
	//  model should call this when its done loading
	_reloading = NO;
	[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
    
	
}

#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
	
    if (_disableTopRefresh == YES) {
        return;
    }
    
	[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
	
    if (_disableTopRefresh == YES) {
        return;
    }
	[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
	
}

#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
	
	[self reloadTableViewDataSource];
	[self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0];
	
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
	
	return _reloading; // should return if data source model is reloading
	
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
	
	return [NSDate date]; // should return date data source was last changed
	
}

#pragma mark - 事件

//加载更多button被点击
- (void)refreshButtonEvent:(id)sender
{
    //将正在更新设为yes，防止重复更新
    _reloading = YES;
    
    //标记此时为加载更多
    _isFooterRefresh = YES;
    
    [_refreshButton removeFromSuperview];
    [_refreshFooterView addSubview:_activity];
    [_refreshFooterView addSubview:_reloadingLabel];
    
    if([_reloadingLabel isHidden])
    {
        [_reloadingLabel setHidden:NO];
    }
    
    [_activity startAnimating];
    
    if (_delegate != nil && [_delegate respondsToSelector:@selector(refresh)]) {
        
        [_delegate refresh];
    }

}

- (void)stopRefresh
{
    //重设_reloading
    _reloading = NO;
    
    if (_disableBottomRefresh == NO) {
        
        [_refreshFooterView addSubview:_refreshButton];
        
        //停止转动
        if ([_activity isAnimating]) {
            [_activity stopAnimating];
        }
        //隐藏"加载中..."
        if(![_reloadingLabel isHidden])
        {
            [_reloadingLabel setHidden:YES];
        }
    }
    
}

@end
